home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
011
/
guard.cq
/
guard.c
Wrap
C/C++ Source or Header
|
1985-06-16
|
2KB
|
89 lines
/* guard.c
*
* from the GUARD programs in Richard Karpinski's article on PARANOIA,
* DDJ, February 1985;
*
* C translation by R. E. Sawyer
*/
#include <math.h>
main()
{
double fabs();
double /*floating-point constants*/
one = 1.,
half = .5,
zero = 0.,
minusOne = -1.;
double
radix, /*calculated floating-point radix*/
precision, /*significant digits in base radix*/
width, /*radix^precision*/
wide, /*first estimate of width*/
ulpOne, /*unit in last place of just less than one*/
ulpRadix, /*radix * ulpOne*/
oneMinus, /*one - ulpOne (calculated with care)*/
radixMinus, /*radix - ulpRadix*/
s, t, u, x, y, z;/*working variables*/
/*find wide so big that adding one does not change it by one*/
wide = one;
do
{
wide += wide;
x = wide + one;
y = x - wide;
z = y - one;
} while (minusOne + fabs(z) < zero);
/*find the radix (or number base) as the minimum increase in wide
* -- since wide is just large enough that the units place is not
* represented, a one in the last represented place is exactly
* the radix
*/
y = one;
do
{
radix = wide + y;
y += y;
radix -= wide;
} while (radix == zero);
printf("\n radix = %.15e", radix);
/*find precision in radix digits*/
precision = zero;
width = one;
do
{
++precision;
width *= radix;
y = width + one;
} while (y - width == one);
printf("\n precision = %.15e", precision);
printf("\n width = %.15e", width);
ulpOne = one / width;
printf("\n closest relative separation found is ulpOne = %.15e",
ulpOne);
oneMinus = (half - ulpOne) + half;
ulpRadix = radix * ulpOne;
radixMinus = radix - one;
radixMinus = (radixMinus - ulpRadix) + one;
x = one - ulpOne;
y = one - oneMinus;
z = one - x;
s = radix - ulpRadix;
t = radix - radixMinus;
u = radix - s;
if ((y == ulpOne) && (z == ulpOne)
&& (t == ulpRadix) && (u == ulpRadix))
printf("\n add/subtract has an appropriate guard digit");
else
printf("\n add/subtract lacks a guard digit");
printf("\n");
}